package com.mazaiting.rabbitmq.listeners;

import com.mazaiting.rabbitmq.callback.IMessageCallback;
import com.mazaiting.rabbitmq.executor.IMessageExecutor;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 动态 MQ 监听
 */
@Slf4j
@Setter
@Component
@RequiredArgsConstructor
public class DynamicMQListener implements ChannelAwareMessageListener {

    /**
     * 消息回调
     */
    private IMessageCallback<Object> messageCallback;

    /**
     * 消息执行器
     */
    private final IMessageExecutor messageExecutor;

    @Override
    public void onMessage(Message message, Channel channel) {
        try {
            // 使用默认的消息执行器
            messageExecutor.execute(message, channel, Object.class, messageCallback);
        } catch (Exception exception) {
            log.error("DynamicMQListener has error，exception=", exception);
        }
    }


}
